package com.boyadegroot.divelog.ui.diver.tabs;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.data.DiveLogData;
import com.boyadegroot.divelog.models.Dive;
import com.boyadegroot.divelog.models.Diver;
import com.boyadegroot.divelog.utilities.DateUtils;

import java.text.NumberFormat;

public class DiverStats extends Fragment {

    private static final String TAG = DiverStats.class.getName();

    private Diver mDiver;

    public static DiverStats newInstance(Diver diver) {
        DiverStats instance = new DiverStats();
        Bundle args = new Bundle();
        args.putParcelable("diver", diver);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mDiver = getArguments().getParcelable("diver");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.pager_diver_tab_statistics, container, false);
        createView(fragmentView);
        return fragmentView;
    }

    private void createView(View fragmentView) {
        DiverStat totalBottomTimeStat = (DiverStat) fragmentView.findViewById(R.id.stat_totalbottomtime);
        DiverStat deepestDiveStat = (DiverStat) fragmentView.findViewById(R.id.stat_deepestdive);
        DiverStat shallowestDiveStat = (DiverStat) fragmentView.findViewById(R.id.stat_shallowestdive);
        DiverStat warmestDiveStat = (DiverStat) fragmentView.findViewById(R.id.stat_warmestdive);
        DiverStat coldestDiveStat = (DiverStat) fragmentView.findViewById(R.id.stat_coldestdive);
        DiverStat minimalSiDiveStat = (DiverStat) fragmentView.findViewById(R.id.stat_minimalsi);

        Activity activity = getActivity();
        DiveLogData data = DiveLogData.getInstance(activity);
        long diverId = mDiver.getId();

        Dive deepestDive = data.getDeepestDive(diverId);
        NumberFormat nf = NumberFormat.getInstance();
        String deepestDiveStatName = getString(R.string.text_deepestdive);
        String deepestDiveStatValue = nf.format(deepestDive.getMaxDepth()) + "m";
        deepestDiveStat.setDiveStat(deepestDive, deepestDiveStatName, deepestDiveStatValue);

        Dive shallowestDive = data.getShallowestDive(diverId);
        String shallowestDiveStatName = getString(R.string.text_shallowestdive);
        String shallowestDiveStatValue = nf.format(shallowestDive.getMaxDepth()) + "m";
        shallowestDiveStat.setDiveStat(shallowestDive, shallowestDiveStatName, shallowestDiveStatValue);

        Dive warmestDive = data.getWarmestDive(diverId);
        String warmestDiveStatName = getString(R.string.text_warmestdive);
        String warmestDiveStatValue = nf.format(warmestDive.getBottomTemp()) + "\u00b0C";
        warmestDiveStat.setDiveStat(warmestDive, warmestDiveStatName, warmestDiveStatValue);

        Dive coldestDive = data.getColdestDive(diverId);
        String coldestDiveStatName = getString(R.string.text_coldestdive);
        String coldestDiveStatValue = nf.format(coldestDive.getBottomTemp()) + "\u00b0C";
        coldestDiveStat.setDiveStat(coldestDive, coldestDiveStatName, coldestDiveStatValue);

        int totalBottomTime = data.getTotalBottomTime(diverId);
        String totalBottomTimeStatName = getString(R.string.text_totalbottomtime);
        String totalBottomTimeStatValue = DateUtils.getDurationString(totalBottomTime, true, false);
        totalBottomTimeStat.setDiveStat(null, totalBottomTimeStatName, totalBottomTimeStatValue);

        Dive minimalSiDive = data.getMinimalSi(diverId);
        int surfaceInterval = minimalSiDive.getSurfaceInterval();
        String minimalSiDiveStatName = getString(R.string.text_minimalsi);
        String minimalSiDiveStatValue = DateUtils.getDurationString(surfaceInterval, false, true);
        minimalSiDiveStat.setDiveStat(minimalSiDive, minimalSiDiveStatName, minimalSiDiveStatValue);

    }
}